package com.zboy.zboycodeutil.templet;

import java.io.*;

public class FileUtil {

    public static String getContent(String fileName) throws Exception {
        File file = new File(fileName);
        String result = "";
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
        BufferedReader read = new BufferedReader(isr);
        String s = null;
        while ((s = read.readLine()) != null) {// 使用readLine方法，一次读一行
            result = result + s + "\n";
        }
        read.close();
        return result;

    }

    public static void setContent(String filePath, String content) throws Exception {
        File f = new File(filePath);
        if (f.exists()) {
            f.delete();
        }
        File pathf = f.getParentFile();
        if (!pathf.exists()) {
            pathf.mkdirs();
        }
        f.createNewFile();// 不存在则创建
        FileOutputStream fout = new FileOutputStream(f);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fout, "UTF-8"));
        writer.write(content);
        writer.close();
        System.out.println("生成代码：" + filePath);
    }

    public static boolean isTemplatFile(String filename) throws Exception {
        String extensionName = getExtensionName(filename);
        if (extensionName.equals("html")) {
            return true;
        }
        if (extensionName.equals("java")) {
            return true;
        }
        if (extensionName.equals("xml")) {
            return true;
        }
        if (extensionName.equals("htm")) {
            return true;
        }
        if (extensionName.equals("jsp")) {
            return true;
        }
        if (extensionName.equals("project")) {
            return true;
        }
        if (extensionName.equals("component")) {
            return true;
        }
        if (extensionName.indexOf("(TFILE)") > 0) {
            return true;
        }
        return false;
    }

    /**
     * Java文件操作 获取文件扩展名
     * <p>
     * Created on: 2011-8-2 Author: blueeagle
     */
    public static String getExtensionName(String filename) throws Exception {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */
    public static void copyFile(String oldPath, String newPath) throws Exception {
        int bytesum = 0;
        int byteread = 0;
        File oldfile = new File(oldPath);
        if (oldfile.exists()) { // 文件存在时
            //创建新的文件的文件夹
            File newpath = new File(newPath);
            if (!newpath.getParentFile().exists()) {
                newpath.getParentFile().mkdirs();
            }
            InputStream inStream = new FileInputStream(oldPath); // 读入原文件
            FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            //int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread; // 字节数 文件大小
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
        }
    }

}
